package image

import org.apache.poi.poifs.filesystem.*
import java.io.*


fun main(args: Array<String>) {
//        create()
    parse()

//    val file = File("F:\\master.eif")
//    println(file.length())
//        read()

}

private fun read() {
    val fs = POIFSFileSystem(FileInputStream("F:\\DownLoad\\all.eif"))
    var root = fs.root
    for (entry in root) {
        println("+++++++++++++++++=>" + entry.name)
        if (entry is DirectoryEntry) {
            for (e in entry) {
                println(e.name)
            }
        }
    }
}

private fun create() {
    val file = File("F:\\1.eif")
    val fs = POIFSFileSystem.create(file)
    val directory = fs.createDirectory("directory1")
    directory.createDocument("entry1.png", FileInputStream("F:\\star.png"))
    fs.writeFilesystem()
}

var fileCount = 0

var parsedFileCount = 0

private fun parse() {
    val fs: POIFSFileSystem
    val destFolder = File("F:\\EIF_EXTRACT_FOLDER")
    if (destFolder.exists().not())
        destFolder.mkdirs()
    try {
        val source = FileInputStream("F:\\DownLoad\\我的收藏.eif")
        fs = POIFSFileSystem(source)
        val root = fs.root


        for (entry in root) {
            if (entry is DirectoryEntry) {
                for (e in entry) {
                    if (e is DocumentEntry && e.name.endsWith(".dat").not())
                        fileCount++
                }
            } else if (entry is DocumentEntry && entry.name.endsWith(".dat").not()) {
                fileCount++
            }
        }
        println("共计 $fileCount 个sticker")

        val startTime = System.currentTimeMillis()
        for (entry in root) {
            if (entry is DirectoryEntry) {
                val dir = File(destFolder, entry.name)
                if (dir.exists().not())
                    dir.mkdirs()
//                    println("directory name: " + entry.name)
                for (e in entry) {
                    if (e.name.contains(".dat").not() && e.isDocumentEntry) {
//                            println("\t directory: ${entry.name} ---document name:${e.name}")
                        val stream = (entry as DirectoryNode).createDocumentInputStream(e.name)
                        saveFromStream(stream, dir, e.name)
                        parsedFileCount++
                        println("已经导入 $parsedFileCount / $fileCount")
                    } else {
//                            saveFromStream(fs.createDocumentInputStream(e.name), File(destFolder, entry.name), e.name)
                    }
                }

            } else if (entry is DocumentEntry) {
                println(entry.name + "---")
                val documentInputStream = fs.createDocumentInputStream(entry.name)
//                    println("name:${entry.name} file size: ${entry.size} bytes" + "file stream available: " + documentInputStream.available() + "bytes")
                saveFromStream(documentInputStream, destFolder, entry.name)
            }
        }
        println("花费时间： ${System.currentTimeMillis() - startTime}")

    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

private fun saveFromStream(inputStream: InputStream, dir: File, name: String) {
    if (File(dir, name).exists()) {
        println("----------------------------------")
        return
    }
    try {
        val bufOutputStream = BufferedOutputStream(FileOutputStream(File(dir, name)))
        if (dir.exists().not())
            dir.mkdirs()
        var len = -1
        val buf = ByteArray(4096)
        while (inputStream.read(buf).also { len = it } != -1) {
            bufOutputStream.write(buf, 0, len)
            bufOutputStream.flush()
        }
        bufOutputStream.close()
        inputStream.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }

}

